const fs = require('fs');
const http = require('http');

// 读取进货单数据文件
const rawData = fs.readFileSync('./通过插件拦截响应数据（淘宝进货单）.md', 'utf8');

// 解析JSON数据
const jsonData = JSON.parse(rawData);

// 修改数据以确保是新的订单
// 修改批次ID和时间戳
jsonData.batchId = 'batch_test_shop_name_' + Date.now();
jsonData.timestamp = Date.now();

// 准备请求选项
const options = {
  hostname: '127.0.0.1',
  port: 9001,
  path: '/api/v1/plugin/data/receive',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': Buffer.byteLength(JSON.stringify(jsonData)),
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
  }
};

// 发送POST请求到API端点
const req = http.request(options, (res) => {
  let responseData = '';
  
  res.on('data', (chunk) => {
    responseData += chunk;
  });
  
  res.on('end', () => {
    try {
      const result = JSON.parse(responseData);
      console.log('数据发送成功:', result);
    } catch (error) {
      console.log('响应数据:', responseData);
    }
  });
});

req.on('error', (error) => {
  console.error('发送数据时出错:', error);
});

// 发送请求体
req.write(JSON.stringify(jsonData));
req.end();